﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace RCSP.DataAccess
{
    public class RCSPConnectionManager
    {
        #region Class's MEMBERS

        private string connectionString;
        private static RCSPConnectionManager conManager;

        #endregion

        #region CONSTRUCTORS

        internal RCSPConnectionManager(string connString)
        {
            this.connectionString = connString;
        }


        #endregion

        #region STATIC METHODS

        public static void CreateConnectionManager(string connString)
        {
            conManager = new RCSPConnectionManager(connString);
        }

        public static void ReplaceCurrentInstance(string connString)
        {
            conManager = new RCSPConnectionManager(connString);
        }


        #endregion

        #region Class's PROPERTIES

        public string ConnectionString
        {
            get { return this.connectionString; }
        }

        public static RCSPConnectionManager CurrentInstance
        {
            get
            {
                if (conManager == null)
                    throw new ArgumentNullException("ConnectionManager is not initialized.");

                return conManager;
            }
        }


        #endregion

        #region METHODS:

        public static SqlConnection GetConnection()
        {
            SqlConnection connection = null;
            try
            {
                connection = new SqlConnection(RCSPConnectionManager.CurrentInstance.ConnectionString);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            return connection;
        }


        #endregion
    }
}
